<template>
  <el-header>
    <div class="left">
      <el-icon color="white" :size="20" @click="handleCollapse"
        ><Menu
      /></el-icon>
      <h3>管理系统</h3>
    </div>
    <div class="right">
        <h4 class="userName">欢迎{{ $store.state.userInfo.username }}</h4>
      <el-dropdown>
        <span class="el-dropdown-link">
          <el-icon color="white" :size="20"><Avatar /></el-icon>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item @click="handleCenter">个人中心</el-dropdown-item>
            <el-dropdown-item @click="handleLogout">退出</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </el-header>
</template>
<script setup>
import { Menu, Avatar } from "@element-plus/icons-vue";
import { useRouter } from "vue-router";
import store from "../../store/index";

const router = useRouter();
const handleCollapse = () => {
  store.commit("changeCollapse");
};
const handleCenter = () => {
  router.push("/center");
};

const handleLogout = () => {
  //清除token
  localStorage.removeItem("token");
  router.push("/login");
  store.commit('clearUserInfo');
};
</script>

<style lang="scss" scoped>
.el-header {
  color: #fff;
  display: flex;
  justify-content: space-between;
  width: 100%;
  height: 64px;
  line-height: 64px;
  text-align: center;
  background-color: #2d3a4b;
  h3 {
    margin-left: 50px;
  }
  .left {
    display: flex;
  }
  .left {
    i {
      margin: auto;
    }
  }

  .right {
    position: relative;
    .userName {
        position: absolute;
        right: 20px;
        width: 200px;
    }
    .el-dropdown {
      position: absolute;
      top: 50%;
      right: 30px;
      transform: translate(-50%, -50%);
    }
  }
}
</style>